﻿////初始化
$(function () {
    $(document).on('click.bs.modal.data-api', '[data-toggle="collapse"]', function (e) {

        if ($(this).find(".glyphicon-collapse-down").html() != undefined) {
            $(this).find(".glyphicon-collapse-down").removeClass("glyphicon-collapse-down").addClass("glyphicon-collapse-up");
        }
        else if ($(this).find(".glyphicon-collapse-up").html() != undefined) {
            $(this).find(".glyphicon-collapse-up").removeClass("glyphicon-collapse-up").addClass("glyphicon-collapse-down");
        }
        else { }

    });

    //模态对话框
    $(document).on('click.bs.modal.data-api', '[data-toggle="x-modal"]', function (e) {
        var $this = $(this);
        var href = $this.attr('href');
        if (href != undefined && href != null && href != "") {

            var ModalID = $this.attr('data-modalId'); // guidGenerator(); 修复 MVC Ajax BUG
            if (ModalID != undefined && ModalID != null && ModalID != "") {

                if (href.indexOf("?") == -1) {
                    href = href + "?" + "ModalID=" + ModalID;
                }
                else {
                    href = href + "&" + "ModalID=" + ModalID;
                }

                $.get(href, function (data, status) {
                    if (status == "success") {
                        $("body").append('<div class="modal fade" id="' + ModalID + '" tabindex="-1" role="dialog" aria-hidden="true"></div>');
                        $('#' + ModalID).empty().append(data);

                        modalload(ModalID);

                    }
                });
            }
            else {
                alert("data-modalId 没有设置");
            }

        }
        else {
            alert("链接为空!");
        }

        return false;
    });


    //提示框绑定HTML事件
    $(document).on('click.bs.modal.data-api', '[data-toggle="x-alert"]', function () {
        var $this = $(this)

        var option = {};

        option.title = $this.attr('data-title');

        option.message = $this.attr('data-message');

        xalert(option);

        return false;
    });

});

function talert(message) {
    var title = "提示";
    if (arguments.length == 1) {
        xalert({ message: title });
    } else {
        xalert({ title: title, message: message });
    }
}

function talert(title, message) {
    if (arguments.length == 1) {
        xalert({ message: title });
    } else {
        xalert({ title: title, message: message });
    }
}


//替换当前模态框
function ralert(title, message) {
    if ($("body").find(".modal-dialog").html() != undefined) {

        var modal_dialog = $("body").find(".modal-dialog");

        modal_dialog.removeAttr("style").width(400);

        if (arguments.length == 1) {
            modal_dialog.find(".modal-body").removeAttr("style").html(title);
        } else {

            modal_dialog.find(".modal-title").empty().append(title);
            modal_dialog.find(".modal-body").removeAttr("style").empty().append(message);
        }

        if (modal_dialog.find(".modal-footer").html() == undefined) {
            modal_dialog.find(".modal-content").append('<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div>');
        }
        else {
            modal_dialog.find(".modal-footer").html('<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>');
        }
    }
    else {
        if (arguments.length == 1) {
            xalert({ message: title });
        } else {
            xalert({ title: title, message: message });
        }
    }
}


/// <summary>
/// 当前模态框内容前加入消息
/// </summary>
/// <param name="message">内容</param>
/// <param name="classname">消息样式类（alert-success/alert-info/alert-warning/alert-danger）</param>
function rmsg(message, classname) {

    if (arguments.length == 1) {
        classname = "";
    }

    if ($("body").find(".modal-dialog").html() != undefined) {

        var modal_dialog = $("body").find(".modal-dialog");

        if (modal_dialog.find(".modal-body").children().first().hasClass("alert")) {
            modal_dialog.find(".modal-body").children().first().replaceWith('<div class="alert ' + classname + '">' + message + '</div>');
        }
        else {
            modal_dialog.find(".modal-body").prepend('<div class="alert ' + classname + '">' + message + '</div>');
        }
    }
}

//清除消息
function clear_rmsg() {

    if ($("body").find(".modal-dialog").find(".modal-body").children().first().hasClass("alert")) {
        $("body").find(".modal-dialog").find(".modal-body").children().first().remove();
    }

}

//提示框事件
function xalert(e) {

    if (e == undefined || e == null) {
        e = {};
    }

    if (e.title == undefined || e.title == null) {
        e.title = "提示";
    }

    if (e.message == undefined || e.message == null) {
        e.message = "...";
    }

    var ModalID = guidGenerator();

    $("body").append('<div class="modal fade" id="' + ModalID + '" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog" style="width:400px;"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">' + e.title + '</h4></div><div class="modal-body"><p>' + e.message + '</p></div><div class="modal-footer"><button type="button" class="btn btn-sm btn-default" data-dismiss="modal">关闭</button></div></div></div></div>');

    modalload(ModalID);
}

//模态框加载初始化
function modalload(ModalID) {

    //    //居中特效
    //    $('#' + ModalID).on('shown.bs.modal', function () {
    //        var ph = ($(this).height() - $(this).find(".modal-dialog").height()) / 2;

    //        $(this).css({
    //            'padding-top': function () {
    //                return (ph - 30);
    //            }
    //        });
    //    });

    $('#' + ModalID).on('hidden.bs.modal', function () {
        $('#' + ModalID).remove();
    });
    $('#' + ModalID).modal({ show: true, backdrop: 'static' });
   // $('#' + ModalID).modal('show');
}


//生成Guid
function guidGenerator() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

//去掉多余长度,str 字符串，length 长度(中文2，英文1)
function cutstr(str, length) {

    var r = /[^\x00-\xff]/g;

    if (str.replace(r, "mm").length <= length)
    { return str };

    var m = Math.floor(length / 2);
    for (var i = m; i < str.length; i++) {
        if (str.substr(0, i).replace(r, "**").length >= length) {
            return str.substr(0, i) + "...";
        }
    }

    return str;

}

//获得字符串实际长度，中文2，英文1
function getlength(str) {
    return str.replace(/[^\x00-\xff]/g, "**").length;
};


//模态对话框
function open_modal(href) {
    if (href != undefined && href != null && href != "") {
        var ModalID = guidGenerator();

        if (href.indexOf("?") == -1) {
            href = href + "?" + "ModalID=" + ModalID;
        }
        else {
            href = href + "&" + "ModalID=" + ModalID;
        }

        $.get(href, function (data, status) {
            if (status == "success") {
                $("body").append('<div class="modal fade" id="' + ModalID + '" tabindex="-1" role="dialog" aria-hidden="true"></div>');
                $('#' + ModalID).empty().append(data);

                modalload(ModalID);

            }
        });



    }
    else {
        alert("链接为空!");
    }

    return false;
}